package com.meatball.dronefly.camera;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import com.meatball.dronefly.R;

import java.util.Objects;

import dji.sdk.camera.VideoFeeder;
import dji.sdk.codec.DJICodecManager;

/**
 * 实时图传
 * @author 张翔宇
 * @since 2022年9月2日
 */
public class CameraGimbalActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * FPV显示区域
     */
    private TextureView textureViewFPV;
    /**
     * 返回按钮
     */
    private Button buttonBack;
    /**
     * 编码译码器
     */
    private DJICodecManager djiCodecManager;
    /**
     * VideoFeeder视频流数据监听器
     */
    private VideoFeeder.VideoDataListener videoDataListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_gimbal);
        initUI();
        initListener();
    }

    /**
     * 初始化UI界面
     */
    private void initUI() {
        textureViewFPV = findViewById(R.id.texture_fpv);
        buttonBack = findViewById(R.id.btn_back);
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        textureViewFPV.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int width, int height) {
                // 在SurfaceTexture可用时创建解码译码器
                if (Objects.isNull(djiCodecManager)) {
                    djiCodecManager = new DJICodecManager(CameraGimbalActivity.this, surfaceTexture, width, height);
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
                // 在SurfaceTexture销毁时释放编码译码器
                if (Objects.nonNull(djiCodecManager)) {
                    djiCodecManager.cleanSurface();
                    djiCodecManager = null;
                }
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {

            }
        });

        // 为返回按钮添加监听器
        buttonBack.setOnClickListener(this);

        // 为VideoFeeder视频流数据设置监听
        videoDataListener = (bytes, i) -> {
            if (Objects.nonNull(djiCodecManager)) {
                djiCodecManager.sendDataToDecoder(bytes, i);
            }
        };
        VideoFeeder.getInstance().getPrimaryVideoFeed().addVideoDataListener(videoDataListener);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_back) {
            back();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeListener();
    }

    /**
     * 返回主界面
     */
    private void back() {
        this.finish();
    }

    /**
     * 一处视频监听器
     */
    private void removeListener() {
        // 移除在VideoFeed的视频流数据监听器
        VideoFeeder.getInstance().getPrimaryVideoFeed().removeVideoDataListener(videoDataListener);
    }
}